﻿//======================================================================
//  日期类的扩展类,提供对DateTime类型的一些常用判断和计算方法
//====================================================================== 

using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace Common
{
    /// <summary>
    /// 日期类的扩展类,提供对DateTime类型的一些常用判断和计算方法
    /// </summary>
    public sealed class DateTimePlus
    {
        #region 是否为DateTime类型

        /// <summary>
        /// 字符串是否为DateTime类型
        /// </summary>
        /// <param name="dateString"></param>
        /// <returns></returns>
        public static bool IsDateTime(string dateString)
        {
            if (string.IsNullOrEmpty(dateString))
                return false;

            DateTime dt = DateTime.MinValue;
            return DateTime.TryParse(dateString, out dt);
        }

        /// <summary>
        /// 字符串是否为DateTime类型
        /// </summary>
        /// <param name="dateString"></param>
        /// <returns></returns>
        public static bool IsDateTime(object dateObj)
        {
            if (dateObj == null)
                return false;
            return IsDateTime(dateObj.ToString());
        }

        #endregion

        #region 取每月的第一/最末一天

        /// <summary>
        /// 取每月的第一/最末一天
        /// </summary>
        /// <param name="time">传入时间</param>
        /// <param name="firstDay">第一天还是最末一天</param>
        /// <returns></returns>
        public static DateTime DayOfMonth(DateTime time, bool firstDay)
        {
            DateTime time1 = new DateTime(time.Year, time.Month, 1);
            if (firstDay) return time1;
            else return time1.AddMonths(1).AddDays(-1);
        }

        #endregion

        #region 取每季度的第一/最末一天

        /// <summary>
        /// 取每季度的第一/最末一天
        /// </summary>
        /// <param name="time">传入时间</param>
        /// <param name="firstDay">第一天还是最末一天</param>
        /// <returns></returns>
        public static DateTime DayOfQuarter(DateTime time, bool firstDay)
        {
            int m = 0;
            switch (time.Month)
            {
                case 1:
                case 2:
                case 3:
                    m = 1; break;
                case 4:
                case 5:
                case 6:
                    m = 4; break;
                case 7:
                case 8:
                case 9:
                    m = 7; break;
                case 10:
                case 11:
                case 12:
                    m = 11; break;
            }

            DateTime time1 = new DateTime(time.Year, m, 1);
            if (firstDay) return time1;
            else return time1.AddMonths(3).AddDays(-1);
        }

        #endregion

        #region 取每年的第一/最末一天

        /// <summary>
        /// 取每年的第一/最末一天
        /// </summary>
        /// <param name="time">传入时间</param>
        /// <param name="firstDay">第一天还是最末一天</param>
        /// <returns></returns>
        public static DateTime DayOfYear(DateTime time, bool firstDay)
        {
            if (firstDay) return new DateTime(time.Year, 1, 1);
            else return new DateTime(time.Year, 12, 31);
        }

        #endregion

        #region 返回当前日期的标准格式string(yyyy-MM-dd)

        /// <summary>
        /// 返回当前日期的标准格式string(yyyy-MM-dd)
        /// </summary>
        public static string GetNowDate()
        {
            return DateTime.Now.ToString("yyyy-MM-dd");
        }

        #endregion

        #region 返回当前时间标准格式string(HH:mm:ss)

        /// <summary>
        /// 返回当前时间标准格式string(HH:mm:ss)
        /// </summary>
        public static string GetNowTime()
        {
            return DateTime.Now.ToString("HH:mm:ss");
        }

        #endregion

        #region 返回当前日期时间的标准格式string(yyyy-MM-dd HH:mm:ss)

        /// <summary>
        /// 返回当前日期时间的标准格式string(yyyy-MM-dd HH:mm:ss)
        /// </summary>
        public static string GetNowDateTime()
        {
            return DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
        }

        #endregion

        #region 当前日期 + 指定天数

        /// <summary>
        /// 当前日期 + 指定天数
        /// </summary>
        public static string GetNowAddDays(int days)
        {
            return DateTime.Now.AddDays(days).ToString("yyyy-MM-dd HH:mm:ss");
        }

        #endregion

        #region 返回当前日期时间的标准格式string(yyyy-MM-dd HH:mm:ss:fffffff)

        /// <summary>
        /// 返回当前日期时间的标准格式string(yyyy-MM-dd HH:mm:ss:fffffff)
        /// </summary>
        public static string GetDateTimeF()
        {
            return DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fffffff");
        }

        #endregion

        #region 返回某个日期对象的日期格式字符串
        public static string GetShortDateString(Object obj)
        {
            string datestring = string.Empty;
            DateTime date=new DateTime();
            if (obj != null)
            {
                DateTime.TryParse(obj.ToString(), out date);
                datestring = date.ToString("yyyy-MM-dd");
            }
            return datestring;
        }
        #endregion

        #region 将Dunull字段赋值
        /// <summary>
        /// 将Dunull字段赋值转为时间
        /// </summary>
        /// <param name="time"></param>
        /// <returns></returns>
        public static DateTime ToTime(object time)
        {
            DateTime dt = DateTime.Parse("1900-1-1");
            if (time==null || DBNull.Value==time)
            {
                return dt;
            }
            if (IsDateTime(time))
            {
                dt = DateTime.Parse(time.ToString());
            }
            return dt;
        }
        #endregion

        #region 返回年月日时分
        /// <summary>
        /// 返回年月日时分(2011-3-10 14:00)
        /// </summary>
        /// <param name="NewTakeOffTime"></param>
        /// <returns></returns>
        public static string ShowShortTime(string NewTakeOffTime)
        {
            string strTime = "1900-1-1";
            if (!string.IsNullOrEmpty(NewTakeOffTime))
            {
                DateTime dtnew = Convert.ToDateTime(NewTakeOffTime);
                strTime = string.Format("{0:g}", dtnew);
            }
            return strTime;
        }
        #endregion

        #region 显示年月日
        /// <summary>
        /// 2005年11月5日
        /// </summary>
        /// <param name="TimeIn"></param>
        /// <returns></returns>
        public static string showYYMMDD(object TimeIn)
        {
            if (DBNull.Value == TimeIn)
            {
                return "1900-1-1";
            }
            string strTime = "1900-1-1";
            if (!string.IsNullOrEmpty(TimeIn.ToString()))
            {
                DateTime dtnew = Convert.ToDateTime(TimeIn.ToString());
                strTime = string.Format("{0:D}", dtnew);
            }
            return strTime;
        }
        /// <summary>
        /// 显示年月日
        /// 2011-11-5
        /// </summary>
        /// <param name="TimeIn"></param>
        /// <returns></returns>
        public static string Showyymmdd(object TimeIn)
        {
            if (DBNull.Value == TimeIn)
            {
                return "1900-1-1";
            }
            string strTime = "1900-1-1";
            if (!string.IsNullOrEmpty(TimeIn.ToString()))
            {
                DateTime dtnew = Convert.ToDateTime(TimeIn.ToString());
                strTime = string.Format("{0:d}", dtnew);
            }
            return strTime;
        }
        #endregion

        #region 用n天m小时k分来显示
        /// <summary>
        /// n天m小时k分来显示(14:23:23)
        /// </summary>
        /// <param name="time"></param>
        /// <returns></returns>
        public static string ShowDayMs(object time)
        {
            string strTime = "00:00 00";
            if (DBNull.Value != time)
            {
                DateTime dt = Convert.ToDateTime(time);
                strTime = string.Format("{0:T}", dt);//14:23:23
            }
            return strTime;
        }
        #endregion

        #region  Add By Fhd as 2010-11-02for 日期补0格式
        /// <summary>
        /// 日期补0格式
        /// </summary>
        /// <param name="datetime"></param>
        /// <returns></returns>
        public static string ChangeDateTime(string datetime)
        {
            if (!string.IsNullOrEmpty(datetime))
            {
                DateTime dtime = DateTime.Parse(datetime);
                string year = dtime.Year.ToString();
                string month = string.Empty;
                string day = string.Empty;
                string newdate = string.Empty;
                if (dtime.Month.ToString().Length == 1)
                {
                    month = "0" + dtime.Month;
                }
                else
                {
                    month = dtime.Month.ToString();
                }
                if (dtime.Day.ToString().Length == 1)
                {
                    day = "0" + dtime.Day;
                }
                else
                {
                    day = dtime.Day.ToString();
                }
                return year + "-" + month + "-" + day;
            }
            else
            {
                return "1900-01-01";
            }
        }
        #endregion
    }
}
